const Koa = require('koa');
const koaStatic = require('koa-static');
const route = require('./lib/lib_route.js');
const koaBody = require('koa-body');
const helmet = require('koa-helmet');
const session = require('./lib/lib_session.js');
//const mongodb = require('./lib/lib_mongodb.js');
const level=require('./lib/lib_leveldb.js');
const xtpl = require('koa-xtpl');
const app = new Koa();
const levelLib=require('level');

leveldb = level(levelLib('./db/siteData'));
sessionDB = level(levelLib('./db/session'));
UPLOAD = require('./lib/lib_upfile.js');




(async function () {
    /*****************全局设置 start *************/
    AppPath = __dirname;//app本地路径
   /* let mongo = await mongodb();
    DB = mongo.dbOperate;//mongodb操作对象
    MongoDB = mongo.mongodb;//原生mongodb数据库连接对象*/
    SESSION = session.sessionOperate;//session操作对象
    app.use(xtpl({
        root: AppPath + '/view' , //配置模板目录，指向工程的view目录
        extname:'html'//后缀名
    }));
    /*****************全局设置 end*************/


    //app.keys = ["xeaqscdzw"];
    app.use((ctx,next)=>{CTX=ctx;return next();});
    app.use(koaBody({
        multipart:true, // 支持文件上传
        encoding:'gzip',
        formidable:{
           //uploadDir: AppPath+"/upfile/",// 设置文件上传目录
            keepExtensions: true, // 保持文件的后缀
            maxFieldsSize:2 * 1024 * 1024, // 文件上传大小
        }
    }));



    app.use(session.init);//初始化session
    app.use(helmet());
    app.use(koaStatic(__dirname + '/public'));


    app.use(route);
    app.onerror=(err)=>{
        if(err.msg){
            console.log("__________________");
            console.log(new Date().toLocaleString(),err.msg);
        }
    }
    app.listen("8080");
})();
